<template>
  <div class="app-container">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>套餐实例管理</span>
          <el-button type="primary" @click="handleAdd">新增实例</el-button>
        </div>
      </template>

      <!-- 搜索区域 -->
      <el-form :model="queryParams" ref="queryForm" :inline="true">
        <el-form-item label="客户" prop="customerId">
          <el-select v-model="queryParams.customerId" placeholder="请选择客户" clearable style="width: 200px">
            <el-option
              v-for="item in customerOptions"
              :key="item.id"
              :label="item.nickName || item.id"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="套餐名称" prop="packageId">
          <el-select v-model="queryParams.packageId" placeholder="请选择套餐" clearable style="width: 200px">
            <el-option
              v-for="item in packageOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">搜索</el-button>
          <el-button @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 数据表格 -->
      <el-table v-loading="loading" :data="instList">
        <el-table-column label="实例ID" prop="id" width="80" />
        <el-table-column label="套餐名称" prop="packageName" />
        <el-table-column label="客户ID" prop="customerId" />
        <el-table-column label="创建时间" prop="createTime" width="180" />
        <el-table-column label="生效时间" prop="effTime" width="180" />
        <el-table-column label="过期时间" prop="expTime" width="180" />
        <el-table-column label="操作" width="180" align="center">
          <template #default="scope">
            <el-button type="primary" link @click="handleUpdate(scope.row)">修改</el-button>
            <el-button type="danger" link @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页组件 -->
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 添加/修改对话框 -->
      <el-dialog :title="title" v-model="open" width="500px" append-to-body>
        <el-form ref="instForm" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="套餐" prop="packageId">
            <el-select v-model="form.packageId" placeholder="请选择套餐">
              <el-option
                v-for="item in packageOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="客户" prop="customerId">
            <el-select v-model="form.customerId" placeholder="请选择客户">
              <el-option
                v-for="item in customerOptions"
                :key="item.id"
                :label="item.nickName || item.id"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="生效时间" prop="effTime">
            <el-date-picker
              v-model="form.effTime"
              type="datetime"
              placeholder="选择生效时间"
            />
          </el-form-item>
          <el-form-item label="过期时间" prop="expTime">
            <el-date-picker
              v-model="form.expTime"
              type="datetime"
              placeholder="选择过期时间"
            />
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </template>
      </el-dialog>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { listPackage } from '@/api/app-store/package'
import { listPackageInst, getPackageInst, addPackageInst, updatePackageInst, delPackageInst } from '@/api/app-store/inst'
import { listCustomer } from '@/api/app-store/customer'

// 遮罩层
const loading = ref(false)
// 总条数
const total = ref(0)
// 套餐实例表格数据
const instList = ref([])
// 套餐选项
const packageOptions = ref([])
// 客户选项
const customerOptions = ref([])
// 弹出层标题
const title = ref('')
// 是否显示弹出层
const open = ref(false)

// 查询参数
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  customerId: undefined,
  packageId: undefined
})

// 表单参数
const form = ref({
  id: undefined,
  packageId: undefined,
  customerId: undefined,
  effTime: undefined,
  expTime: undefined
})

// 表单校验规则
const rules = ref({
  packageId: [{ required: true, message: '请选择套餐', trigger: 'change' }],
  customerId: [{ required: true, message: '请输入客户ID', trigger: 'blur' }],
  effTime: [{ required: true, message: '请选择生效时间', trigger: 'change' }],
  expTime: [{ required: true, message: '请选择过期时间', trigger: 'change' }]
})

/** 查询套餐实例列表 */
function getList() {
  loading.value = true
  listPackageInst(queryParams.value).then(response => {
    instList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

/** 获取套餐列表 */
function getPackageOptions() {
  listPackage().then(response => {
    packageOptions.value = response.rows
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  queryParams.value = {
    pageNum: 1,
    pageSize: 10,
    customerId: undefined,
    packageId: undefined
  }
  handleQuery()
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = '添加套餐实例'
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const id = row.id
  getPackageInst(id).then(response => {
    Object.assign(form.value, response.data)
    open.value = true
    title.value = '修改套餐实例'
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const ids = [row.id]
  ElMessageBox.confirm('是否确认删除套餐实例编号为"' + ids + '"的数据项?', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(function() {
    return delPackageInst(ids)
  }).then(() => {
    getList()
    ElMessage.success('删除成功')
  })
}

/** 表单重置 */
function reset() {
  form.value = {
    id: undefined,
    packageId: undefined,
    customerId: undefined,
    effTime: undefined,
    expTime: undefined
  }
}

/** 取消按钮 */
function cancel() {
  open.value = false
  reset()
}

/** 提交按钮 */
function submitForm() {
  if (form.value.id) {
    updatePackageInst(form.value).then(response => {
      ElMessage.success('修改成功')
      open.value = false
      getList()
    })
  } else {
    addPackageInst(form.value).then(response => {
      ElMessage.success('新增成功')
      open.value = false
      getList()
    })
  }
}

/** 获取客户列表 */
function getCustomerOptions() {
  listCustomer().then(response => {
    customerOptions.value = response.rows
  })
}

onMounted(() => {
  getList()
  getPackageOptions()
  getCustomerOptions()
})
</script>

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.dialog-footer {
  text-align: center;
}
</style>